package ElectricCraft.misc;

import javax.swing.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;

/**
 * PopupMenuWrapper class helps to construct popup menus.
 */
public final class PopupMenuWrapper extends JPopupMenu {
    private final Map<String, JMenuItem> items = new HashMap<String, JMenuItem> ();

    public JMenuItem createItem(String title) {
        JMenuItem item = new JMenuItem(title);
        add(item);
        items.put(title, item);
        return item;
    }

    public void createItems(String... titles) {
        for (String title : titles) {
            createItem(title);
        }
    }

    public void addActionListener(ActionListener listener) {
        for (JMenuItem item : items.values()) {
            item.addActionListener(listener);
        }
    }

    public boolean checkAction(ActionEvent event, String title) {
        return event.getSource() == items.get(title);
    }
}
